package com.platform.service.impl;

import com.platform.dao.RecommendUserDao;
import com.platform.dao.UserDynamicDao;
import com.platform.entity.*;
import com.platform.service.*;
import com.platform.util.CoinTypeEnum;
import com.platform.util.EnergyCoinTypeEnum;
import com.platform.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * Service实现类
 * @author lipengjun
 * @email 939961241@qq.com
 * @date 2018-08-21 17:19:56
 */
@Service("userDynamicService")
public class UserDynamicServiceImpl implements UserDynamicService {
    @Autowired
    private UserDynamicDao userDynamicDao;
    @Autowired
    private UserAssetService userAssetService;
    @Autowired
    private DynamicCateMappingService dynamicCateMappingService;
    @Autowired
    private RecommendUserDao recommendUserDao;
    @Autowired
    private DynamicRuleService dynamicRuleService;
    @Autowired
    private EnergySpcService energySpcService;
    @Autowired
    private IncomeCoinTaskService incomeCoinTaskService;

    @Override
    public UserDynamicEntity queryObject(Integer id) {
        return userDynamicDao.queryObject(id);
    }

    @Override
    public List<UserDynamicEntity> queryList(Map<String, Object> map) {
        //获取15天前的时间
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - 15);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String result = format.format(today);
        map.put("before", result);

        return userDynamicDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        //获取15天前的时间
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - 15);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String result = format.format(today);
        map.put("before", result);
        return userDynamicDao.queryTotal(map);
    }

    @Override
    public int save(UserDynamicEntity cmsNews) {
        return userDynamicDao.save(cmsNews);
    }

    @Override
    public int update(UserDynamicEntity cmsNews) {
        //后保存
        List<Integer> categoryIDs = cmsNews.getCategoryIDlist();
        DynamicRuleEntity dynamicRuleEntity = dynamicRuleService.selectForKey("choicenessContent");//获取任务
        if(categoryIDs.contains(18)){//精选
            UserAssetEntity userAssetEntity = userAssetService.queryObjectForUid(cmsNews.getAuthorid());
            Integer count = incomeCoinTaskService.queryTotalByParam(userAssetEntity.getUid(),dynamicRuleEntity.getId());//查询当前用户当天是否存在记录
            if(count<dynamicRuleEntity.getLimitnum()){//小于次数  添加
                if(userAssetEntity.getDone()==null||userAssetEntity.getDone()==0){//非Done
                    //添加能量
                    Map<String,Object> addCoin = new HashMap<String, Object>();
                    addCoin.put("coin",dynamicRuleEntity.getCreditcount());
                    addCoin.put("uid",cmsNews.getAuthorid());
                    addCoin.put("version",userAssetEntity.getVersion());
                    int update = userAssetService.addCoin(addCoin);
                    if(update>0){
                        //添加能量收益
                        EnergySpcEntity energyCoinEntity = new EnergySpcEntity();
                        energyCoinEntity.setCreateDate(new Date());
                        energyCoinEntity.setPhone(userAssetEntity.getPhone());
                        energyCoinEntity.setIncomeType(0);//收入
                        energyCoinEntity.setType(CoinTypeEnum.ENERGY_CHOICENESS.getKey());
                        energyCoinEntity.setTypeName(CoinTypeEnum.ENERGY_CHOICENESS.getValue());
                        energyCoinEntity.setUid(userAssetEntity.getUid());
                        energyCoinEntity.setImage(CoinTypeEnum.ENERGY_CHOICENESS.getImage());
                        energyCoinEntity.setEnergyCoin(dynamicRuleEntity.getCreditcount().doubleValue());
                        energyCoinEntity.setInviteUid(Long.valueOf(0));
                        energySpcService.save(energyCoinEntity);
                        //新增记录
                        IncomeCoinTaskEntity incomeCoinTaskEntity = new IncomeCoinTaskEntity();
                        incomeCoinTaskEntity.setCreatedat(new Date());
                        incomeCoinTaskEntity.setCreditruleid(dynamicRuleEntity.getId());
                        incomeCoinTaskEntity.setName(dynamicRuleEntity.getName());
                        incomeCoinTaskEntity.setIntegral(dynamicRuleEntity.getCreditcount());
                        incomeCoinTaskEntity.setUserid(userAssetEntity.getUid());
                        incomeCoinTaskService.save(incomeCoinTaskEntity);
                    }
                }else{//Done
                    //添加能量
                    Map<String,Object> addCoin = new HashMap<String, Object>();
                    addCoin.put("coin",dynamicRuleEntity.getCreditcount().multiply(new BigDecimal(10)));
                    addCoin.put("uid",cmsNews.getAuthorid());
                    addCoin.put("version",userAssetEntity.getVersion());
                    int update = userAssetService.addCoin(addCoin);
                    if(update>0){
                        //添加能量收益
                        EnergySpcEntity energyCoinEntity = new EnergySpcEntity();
                        energyCoinEntity.setCreateDate(new Date());
                        energyCoinEntity.setPhone(userAssetEntity.getPhone());
                        energyCoinEntity.setIncomeType(0);//收入
                        energyCoinEntity.setType(CoinTypeEnum.ENERGY_CHOICENESS.getKey());
                        energyCoinEntity.setTypeName(CoinTypeEnum.ENERGY_CHOICENESS.getValue());
                        energyCoinEntity.setUid(userAssetEntity.getUid());
                        energyCoinEntity.setImage(CoinTypeEnum.ENERGY_CHOICENESS.getImage());
                        energyCoinEntity.setEnergyCoin(dynamicRuleEntity.getCreditcount().doubleValue()*10);
                        energyCoinEntity.setInviteUid(Long.valueOf(0));
                        energySpcService.save(energyCoinEntity);
                        //新增记录
                        IncomeCoinTaskEntity incomeCoinTaskEntity = new IncomeCoinTaskEntity();
                        incomeCoinTaskEntity.setCreatedat(new Date());
                        incomeCoinTaskEntity.setCreditruleid(dynamicRuleEntity.getId());
                        incomeCoinTaskEntity.setName(dynamicRuleEntity.getName());
                        incomeCoinTaskEntity.setIntegral(dynamicRuleEntity.getCreditcount().multiply(new BigDecimal(10)));
                        incomeCoinTaskEntity.setUserid(userAssetEntity.getUid());
                        incomeCoinTaskService.save(incomeCoinTaskEntity);
                    }
                }
            }
            RecommendUserVo recommendUserVo = recommendUserDao.queryObjectByUid(cmsNews.getAuthorid());
            if(recommendUserVo!=null){
                recommendUserVo.setNumber(recommendUserVo.getNumber()+1);
                recommendUserVo.setDid(cmsNews.getId());
                recommendUserVo.setCreateDate(new Date());
                recommendUserDao.update(recommendUserVo);
            }else{
                recommendUserVo = new RecommendUserVo();
                recommendUserVo.setCreateDate(new Date());
                recommendUserVo.setDid(cmsNews.getId());
                recommendUserVo.setUid(cmsNews.getAuthorid());
                recommendUserVo.setNumber(1);
                recommendUserDao.save(recommendUserVo);
            }
//            boolean  result= userAssetService.updateAsset(Long.valueOf(cmsNews.getAuthorid()),Long.valueOf(cmsNews.getId()));
//            if(!result){
//                return -1;
//            }
        }
        //删除关系
        dynamicCateMappingService.delete(cmsNews.getId());
        for(int i=0;i<categoryIDs.size();i++){
            DynamicCateMappingEntity catetgoryNew = new DynamicCateMappingEntity();
            catetgoryNew.setNewsid(cmsNews.getId());
            catetgoryNew.setCategoryid(categoryIDs.get(i));
            dynamicCateMappingService.save(catetgoryNew);
        }

        cmsNews.setNewdes(null);
        return userDynamicDao.update(cmsNews);
    }

    @Override
    public int delete(Integer id) {
        return userDynamicDao.delete(id);
    }

    @Override
    public int deleteBatch(Integer[] ids) {
        return userDynamicDao.deleteBatch(ids);
    }

    @Override
    public int check(Integer[] ids) {
        return userDynamicDao.check(ids);
    }


}
